package org.example;


import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Date;

public class Main {

    static final String lineTxt = " - c++/go/cangjie 测试文本写入文件的速度/性能，需要高频写入日志的场景对写文件性能关注会比较高 - 2024 ++++ c++/go/cangjie 测试文本写入文件的速度/性能，需要高频写入日志的场景对写文件性能关注会比较高 - 2024";

    public static void main(String[] args) {
        final long startTime = System.currentTimeMillis();
        long totalWriteSize = 0;
        for (int i = 0; i < 10; i++) {
            totalWriteSize = stringWriteToFile();
        }
        final long useMs = (System.currentTimeMillis() - startTime) / 10;

        System.out.printf("文件写入完毕,文件总大小：%d - ，平均单次耗时(ms):%d\n", totalWriteSize, useMs);
    }


    static long stringWriteToFile() {
        final String filePath = "D:/写文件/log_java_no_buffer.txt";
        final long rows = 1000000; // 写入100万行文本，模拟写入日志场景
        try {
            File file = new File(filePath);
            try (FileOutputStream writer = new FileOutputStream(file)) {
                for (long i = 1; i <= rows; i++) {
                    writer.write((i + lineTxt + "\n").getBytes());
                }
//                writer.flush();
            }
            return file.length();
        } catch (Exception e) {
            System.out.printf("创建文件出错：%s\n", e);
        }
        return 0;
    }

}
